﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using System;
using Tesla.Core;
using Tesla.Math;

namespace Tesla.Graphics {
    internal struct MaterialEngineParameter {
        private IEffectParameter _effectParameter;
        private String _name;
        private bool _isSemantic;
        private EngineValue _engineValue;

        public IEffectParameter EffectParameter {
            get {
                return _effectParameter;
            }
        }

        public String Name {
            get {
                return _name;
            }
        }

        public bool IsSemantic {
            get {
                return _isSemantic;
            }
        }

        public EngineValue EngineValue {
            get {
                return _engineValue;
            }
        }

        public MaterialEngineParameter(IEffectParameter parameter, String name, bool isSemantic, EngineValue value) {
            _effectParameter = parameter;
            _isSemantic = isSemantic;
            _name = name;
            _engineValue = value;
        }

        public MaterialEngineParameter(MaterialEngineParameter engineParam) {
            _effectParameter = engineParam.EffectParameter;
            _isSemantic = engineParam.IsSemantic;
            _engineValue = engineParam.EngineValue;
            _name = engineParam.Name;
        }

        public MaterialEngineParameter(MaterialEngineParameter engineParam, String name, IEffectParameter replacement) {
            _effectParameter = replacement;
            _isSemantic = engineParam.IsSemantic;
            _engineValue = engineParam.EngineValue;
            _name = name;
        }

        public void Apply(IRenderer renderer) {
            if(_effectParameter != null) {
                switch(_engineValue) {
                    case EngineValue.WorldMatrix:
                    case EngineValue.WorldMatrixInverse:
                    case EngineValue.WorldInverseTranspose:
                    case EngineValue.WorldViewMatrix:
                    case EngineValue.WorldViewProjection:
                    case EngineValue.ViewMatrix:
                    case EngineValue.ViewMatrixInverse:
                    case EngineValue.ViewProjectionMatrix:
                    case EngineValue.ProjectionMatrix:
                    case EngineValue.ProjectionMatrixInverse:
                        Matrix mat;
                        Engine.ValueMap.GetValue(_engineValue, out mat);
                        _effectParameter.SetValue(mat);
                        break;
                    case EngineValue.Viewport:
                        Vector4 vec4;
                        Engine.ValueMap.GetValue(_engineValue, out vec4);
                        _effectParameter.SetValue(vec4);
                        break;
                    case EngineValue.CameraDirection:
                    case EngineValue.CameraRight:
                    case EngineValue.CameraPosition:
                    case EngineValue.CameraUp:
                        Vector3 vec3;
                        Engine.ValueMap.GetValue(_engineValue, out vec3);
                        _effectParameter.SetValue(vec3);
                        break;
                    case EngineValue.Resolution:
                    case EngineValue.FrustumNearFar:
                        Vector2 vec2;
                        Engine.ValueMap.GetValue(_engineValue, out vec2);
                        _effectParameter.SetValue(vec2);
                        break;
                    case EngineValue.Time:
                    case EngineValue.TimePerFrame:
                    case EngineValue.FrameRate:
                    case EngineValue.RandomFloat:
                        float fValue;
                        Engine.ValueMap.GetValue(_engineValue, out fValue);
                        _effectParameter.SetValue(fValue);
                        break;
                    case EngineValue.RandomInt:
                        int iValue;
                        Engine.ValueMap.GetValue(_engineValue, out iValue);
                        break;
                }
            }
        }
    }
}
